#include "stdafx.h"
#include "tinyxml/tinyxml.h"
#include "XmlParse.h"

CXmlParse::CXmlParse(void) : m_nCloseButtonNum(0), m_nMaximizeButtonNum(0), m_nMinimizeButtonNum(0), m_nButtonNum(0)
{
}

CXmlParse::~CXmlParse(void)
{
}

CXmlParse::CXmlParse(const CString & strPath) : m_strXMLPath(strPath), m_nCloseButtonNum(0), m_nMaximizeButtonNum(0), m_nMinimizeButtonNum(0), m_nButtonNum(0)
{
	
}

BOOL CXmlParse::LoadAllParam()
{
	if (m_strXMLPath.IsEmpty() || !::PathFileExists(m_strXMLPath))
	{
		ATLTRACE(_T("path is invalid!"));
		return FALSE;
	}

	USES_CONVERSION;
	TiXmlDocument xmlDoc( W2A(m_strXMLPath) );
	bool bRet = xmlDoc.LoadFile();
	if ( !bRet )
	{
		return FALSE; 
	}

	TiXmlElement *pXmlRootElem = xmlDoc.RootElement();
	CString strValue = pXmlRootElem->Value();
	if (strValue.Compare(A2W("skin")))
	{
		return FALSE;
	}

	m_strSkinPath = CA2T(pXmlRootElem->Attribute("skinpath"), CP_UTF8);
	if (m_strSkinPath.IsEmpty())
	{
		return FALSE;
	}
	//////////////////////////////////////////////////////////////////////////////
	TiXmlElement *pTiElement = NULL;
	pTiElement = pXmlRootElem->FirstChildElement("MainFrame");
	if (pTiElement)
	{
		m_strBackground = m_strSkinPath + _T("\\") + CA2T(pTiElement->Attribute("Background"), CP_UTF8);
		m_strLayer = m_strSkinPath + _T("\\") + CA2T(pTiElement->Attribute("Layer"), CP_UTF8);
		m_strButton	= m_strSkinPath + _T("\\") + CA2T(pTiElement->Attribute("Button"), CP_UTF8);
	
		pTiElement->Attribute("ButtonNum", &m_nButtonNum);
	}
	//////////////////////////////////////////////////////////////////////////////
	pTiElement = pXmlRootElem->FirstChildElement("TitleBar");
	if (pTiElement)
	{
		m_strTitleClose = m_strSkinPath + _T("\\") + CA2T(pTiElement->Attribute("CloseButton"), CP_UTF8);
		pTiElement->Attribute("CloseButtonNum", &m_nCloseButtonNum);

		m_strTitleMin = m_strSkinPath + _T("\\") + CA2T(pTiElement->Attribute("MinimizeButton"), CP_UTF8);
		pTiElement->Attribute("MinimizeButtonNum", &m_nMinimizeButtonNum);

		m_strTitleMax	= m_strSkinPath + _T("\\") + CA2T(pTiElement->Attribute("MaximizeButton"), CP_UTF8);
		pTiElement->Attribute("MaximizeButtonNum", &m_nMaximizeButtonNum);

		CString strColor = CA2T(pTiElement->Attribute("TextColor"), CP_UTF8);
		m_colorTitleText = ConvertStr2Color(strColor);
	}

	return TRUE;

}

COLORREF CXmlParse::ConvertStr2Color(const CString & strCol)
{
	CString strColor = strCol;

	WORD		rgb[3]	= {0};
	COLORREF	col	= RGB(255,255,255);

	strColor.MakeLower();
	if(strColor.Find(L"0x",0)==0)
		strColor.Delete(0,2);

	if(strColor.GetLength()==8)
		strColor.Delete(2);
	if(strColor.GetLength()!=6)
		return col;

	for(int i = 0; i < 3; ++i)
	{
		if(strColor[i*2] >= 'a' && strColor[i*2] <= 'f')
			rgb[i] = 10+strColor[i*2] - 'a';
		else if(strColor[i*2] >= '0' && strColor[i*2] <= '9')
			rgb[i] = strColor[i*2] - '0';
		else
			return col;

		rgb[i] = rgb[i]<<4;

		if(strColor[i*2+1] >= 'a' && strColor[i*2+1] <= 'f')
			rgb[i] |= 10+strColor[i*2+1] - 'a';
		else if(strColor[i*2+1]>= '0' && strColor[i*2+1] <= '9')
			rgb[i] |= strColor[i*2+1] - '0';
		else
			return col;
	}

	col = RGB(rgb[2],rgb[1],rgb[0]);
	return col;
}